package com.demo.controller;

import com.demo.controller.base.BaseCtrl;
import com.demo.controller.model.RSResult;
import com.demo.core.ActionContext;
import com.demo.po.ServerPo;
import com.jcraft.jsch.Session;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/connect")
public class ConnectController extends BaseCtrl {

    @RequestMapping("/test")
    public RSResult testConnect(@RequestBody ServerPo server) {
        log.debug("Start test server connection. params : {}", server);
        RSResult result = new RSResult();
        String host = server.getHost();
        if (StringUtils.isEmpty(host)) {
            result.setResult(-1, "服务器IP不能为空！");
            return result;
        }
        if (server.getSshport() == null || server.getSshport() == 0) {
            result.setResult(-1, "服务器端口号不能为空！");
            return result;
        }
        if (StringUtils.isBlank(server.getSshname()) || StringUtils.isBlank(server.getSshpwd())) {
            result.setResult(-1, "服务器用户名和密码不能为空！");
            return result;
        }
        Session session = ActionContext.getSession(server);
        if (session == null) {
            result.setResult(-1, "连接失败");
        } else {
            result.setResult(0);
        }
        log.debug("End test server connection.");
        return result;
    }

}
